package org.weed.remote.network;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

import org.weed.remote.service.DeviceServiceImpl;
import org.weed.remote.util.ByteUtils;

public class RemoteClient {
	private static final String CHARSET = "ISO-8859-1";
	private InetSocketAddress address;
	private SocketChannel channel;
	private CharsetEncoder encoder;
	private CharsetDecoder decoder;
	private DeviceServiceImpl robot;
	
	public RemoteClient(String host, int port) {
		address = new InetSocketAddress(host, port);
		Charset charset = Charset.forName(CHARSET);
		encoder = charset.newEncoder();
		decoder = charset.newDecoder();
		try {
			robot = new DeviceServiceImpl();
		} catch (Exception e) {
			
		}
	}
	
	public boolean connect() {
		try {
			channel = SocketChannel.open();
			channel.connect(address);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public void disconnect() {
		try {
			channel.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void write(SocketChannel channel, byte bits) throws Exception {
		write(channel, new byte[] {bits});
	}
	
	public void write(SocketChannel channel, byte[] bytes) throws Exception {
		channel.write(ByteBuffer.wrap(bytes));
	}
	
	public void sendMessage(String message) throws IOException {
		System.out.println("@"+message+"@");
		ByteBuffer buffer = encoder.encode(CharBuffer.wrap(message));
		System.out.println("Wrote " + channel.write(buffer) + " bytes.");
	}
	
	public void sendMouseInfo() throws IOException {
		Point position = MouseInfo.getPointerInfo().getLocation();
		int x = position.x;
		int y = position.y;
		sendMessage("X:" + x + ";Y:" + y+"\n");
	}
	
	public void spyMonitor() throws Exception {
		while (true) {
			write(channel, (byte) 0);
			byte[] bytes = ByteUtils.toBytes((robot.screenShot()));
			write(channel, ByteUtils.toBytes(bytes.length));
			write(channel, bytes);
		}
	}
	
	
	public static void main(String[] args) throws Exception {
		RemoteClient client = new RemoteClient("localhost", 8888);
		if (client.connect()) {
			
			client.spyMonitor();
			
			System.err.println("Server connected.");
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
			String command;
			while ((command = input.readLine()) != null && command.length() > 0) {
				if ("exit".equals(command)) {
					client.disconnect();
					System.err.println("Exit");
					System.exit(0);
				} else if ("mouse".equals(command)) {
					client.sendMouseInfo();
				} else if ("spy".equals(command)) {
					client.spyMonitor();
				} else {
					client.sendMessage(command);
				}
			}
		} else {
			System.err.println("Unable to connect to server.");
		}
	}
}
